package com.superdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.superdp.dto.Result;
import com.superdp.dto.UserDTO;
import com.superdp.entity.Follow;
import com.superdp.mapper.FollowMapper;
import com.superdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.superdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    UserServiceImpl userService;
    /**
     * 关注或者取关用户
     * @param id
     * @param isFollow
     * @return
     */
    @Override
    public Result follow(Long id, Boolean isFollow) {
        //1.获取当前用户id
        Long userId = UserHolder.getUser().getId();
        String key="follow:"+userId;
        //2.判断是否关注还是取关
        //2.1关注
        if(isFollow){
            //3.1存入tb_follow
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(id);
            boolean isSuccess = save(follow);
            if(isSuccess){
                //4.存入set集合

                stringRedisTemplate.opsForSet().add(key,id.toString());
            }
        }else {
            //2.2取关
            //3.2从tb_follow中删除
            boolean isSuccess = remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", id));
            if(isSuccess){
                stringRedisTemplate.opsForSet().remove(key,id.toString());
            }
        }
        return Result.ok();
    }

    /**
     * 当前用户是否关注了查询的该用户
     * @param id
     * @return
     */
    @Override
    public Result followOrNot(Long id) {
        //1.获取用户id
        Long userId = UserHolder.getUser().getId();
        //2.根据用户id，要查询的用户id查询tb_follow表
        Integer count = query().eq("user_id", userId).eq("follow_user_id", id).count();
        return Result.ok(count>0);
    }

    /**
     * 获取共同关注
     * @param id
     * @return
     */
    @Override
    public Result getCommon(Long id) {
        //1.获取用户userId
        Long userId = UserHolder.getUser().getId();
        //2.使用redis的set中获取交集id
        String key1="follow:"+userId;
        String key2="follow:"+id;
        Set<String> commonId = stringRedisTemplate.opsForSet().intersect(key1,key2);
        if(commonId==null||commonId.isEmpty()){
            return Result.ok(Collections.emptyList());
        }
        //3.根据id查询用户后封装成UserDTO
        List<Long> ids = commonId.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> userDTOS = userService.listByIds(ids).stream()
                .map(user ->
                        BeanUtil.copyProperties(user, UserDTO.class)
                ).collect(Collectors.toList());
        return Result.ok(userDTOS);
    }
}
